#include "tmp141.h"
#include "systick.h"

char EP = 0;


void Onewire_OUT_PULL_UP(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_Init(GPIOC, &GPIO_InitStruct);
}

void Onewire_OUT_FLOATING(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_Init(GPIOC, &GPIO_InitStruct);
}

void Onewire_IN_FLOATING(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_Init(GPIOC, &GPIO_InitStruct);
}

void Onewire_IN_PULL_UP(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_Init(GPIOC, &GPIO_InitStruct);
}


void Send_Start(void)
{
    Onewire_OUT_PULL_UP();
    SET_DQ();
    Delay_us(11);          //11us
    CLR_DQ();
    Delay_us(95);         //95us
    SET_DQ();
}

void write_bit(unsigned char bitik)
{
    Delay_us(11);         //11us
    CLR_DQ();
    if (bitik == 0)
        Delay_us(15);       //15us
    else
        Delay_us(42);       //42us
    SET_DQ();
}

char get_bit(void)
{
    char flag = 1;
    Delay_us(11);         //11us
    Onewire_OUT_FLOATING();
    //SET_DQ();

    CLR_DQ();


    Delay_us(15);          //15us



    SET_DQ();
    Delay_us(20);

    Onewire_IN_FLOATING();

    if(GET_DQ() == 1)
        flag = 0;
    else
        flag = 1;

    return(flag);
}

unsigned char get_byte(void)
{
    unsigned char byte = 0;
    int i = 7;
    do
    {
        byte |= ((get_bit()) << i);
        i--;
    }
    while(i >= 0);
    return(byte);
}

int get_word(void)
{
    u16 byte_h = ((int)get_byte() << 8);
    u16 byte_l = (int)get_byte();

    return (byte_h | byte_l);
}


char byte_parity(unsigned char device_NO, unsigned char regst_NO, unsigned char R_W, char byte)
{
    char local2;
    int n;
    EP = 0;
    local2 = (device_NO ^ regst_NO ^ R_W ^ byte);
    for(n = 0; n < 8; n++)
        EP = (EP + ((local2 >> n) & 0x01));
    EP = (EP % 2);
    return(EP);
}

char word_parity(unsigned char device_NO, unsigned char regst_NO, unsigned char R_W, int word)
{
    char local1 = 0;
    int  local2 = 0;
    int n;
    EP = 0;
    local1 = (device_NO ^ regst_NO ^ R_W);
    local2 = word ^ (int)local1;
    for(n = 0; n < 16; n++)
        EP = (EP + ((local2 >> n) & 0x0001));
    EP = (EP % 2);
    return(EP);
}

void write_device_regst(unsigned char device_NO, char regst_NO)
{
    unsigned char n;
    unsigned char bitss;

    for (n = 2; n != 0xff; n--)
    {
        bitss = (device_NO >> n) & 0x01;
        write_bit(bitss);
    }
    for (n = 5; n != 0xff; n--)
    {
        bitss = (regst_NO >> n) & 0x01;
        write_bit(bitss);
    }
}


void Onewire_write_word(char device_NO, char regst_NO, int word)
{
    char flag;
    char n;
    char bitler;
    int kaydirma = 0;
    char sayici = 0;

    do
    {
        EP = word_parity(device_NO, regst_NO, 0x00, word);
        Send_Start();
        write_device_regst(device_NO, regst_NO);
        write_bit(0);
        for (n = 0; n <= 15; n++)
        {
            kaydirma = ((word << n) & 0x8000);
            if(kaydirma & 0x8000)
                bitler = 0x80;
            else
                bitler = 0x00;
            write_bit(bitler);
        }
        write_bit(EP);
        flag = get_bit();


        if(flag == 0)
            sayici++;
    }
    while( (flag == 0) && (sayici <= 10) );

    //   if(sayici >= 10)
    //       SENSOR_KOPTU_FLAG = TRUE;

    return;
}



int Onewire_read_word(char device_NO, char regst_NO)
{
    int word;
    char flag;

    Send_Start();
    write_device_regst(device_NO, regst_NO);
    write_bit(1);
    word = get_word();
    flag = get_bit();

    write_bit(1);
    if(flag == 0)
    {
        //ERROR Handle;
    };
    return(word);
}

void Tmp141_Condig(void)
{
    /* Enable Function 1 (begin temperature measure )*/
    Onewire_write_word(1, 0x05, 0x0010);
    /* Enable Sensor && Enable Attention */
    Onewire_write_word(1, 0x0a, 0x0003);
}

